package com.oriental.manage.core.utils;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by lupf on 2016/4/27.
 */
public class DateUtils {

    /**
     * 默认日期格式
     */
    public static final String DATESHOWFORMAT = "yyyy-MM-dd";
    public static final String DATESPRITFORMAT = "yyyy/MM/dd";
    public static final String DATETIMEFORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DATESHORTFORMAT = "yyyyMMdd";
    public static final String DATEMONTHFORMAT = "yyyyMM";
    //    public static final String DATETIMECFORMAT = "yyyyMMddHHssmm";
    public static final String DATETIMESFORMAT = "yyyyMMddHHmmss";
    /**
     * 日期时间格式：yyyyMMddHHmmss
     */
    public static final String fullPattern = "yyyyMMddHHmmss";
    /**
     * 无秒
     */
    public static final String full_pattern = "yyyyMMddHHmm";

    public static Date parse(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(DATESHORTFORMAT);
        return sdf.parse(date);
    }

    public static Date parse(String date, String datetimesformat) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(datetimesformat);
        return sdf.parse(date);
    }

    public static Date parse(Date date, String pattern) throws ParseException {
        if (null == date) {
            return null;
        }
        if (org.apache.commons.lang.StringUtils.isEmpty(pattern)) {
            pattern = DATESHORTFORMAT;
        }
        String dateStr = DateFormatUtils.format(date, pattern);
        return parse(dateStr, pattern);
    }

    public static String format(Date date, String pattern) {
        return date == null ? null : (new DateTime(date)).toString(pattern);
    }

    public static DateTime parseTime(String date, String pattern) {
        if (StringUtils.isBlank(date)) return null;
        if (fullPattern.equals(pattern)) { // 这段逻辑整合自 gateway-bank 中的 DateUtil
            date = StringUtils.rightPad(date, 14, '0');
        }
        return DateTimeFormat.forPattern(pattern).parseDateTime(date);
    }

    /**
     * 得到今天指定格式的日期
     *
     * @param format 指定格式
     * @return 当天日期
     */
    public static String getToDay(String format) {
        if (StringUtils.isEmpty(format)) {
            format = DATESHORTFORMAT;
        }
        DateTime dt = new DateTime();
        return dt.toString(format);
    }

    /**
     * 日期计算
     *
     * @param date       需要计算的日期
     * @param day        需要往前(负数)或往后(正数)的天数
     * @param inPattern  输入时间的格式
     * @param outPattern 输出时间的格式
     * @return 按照输出时间格式进行格式化后的时间
     */
    public static String computeDate(String date, int day, String inPattern, String outPattern) {
        try {
            DateTime dateTime = parseTime(date, inPattern);
            dateTime = dateTime.minusDays(0 - day);
            return format(dateTime.toDate(), outPattern);
            //输入时间转换错误时返回空值 避免因时间转换错误而导致原流程无法继续执行
        } catch (Exception e) {

            return null;
        }
    }

    public static String now() {
        return new DateTime().toString(DATETIMESFORMAT);
    }

    /**
     * 计算两个日期之间相差的天数(Date类型)
     *
     * @param startDate 开始日期
     * @param endDate   结束日期
     * @return 相差天数
     */
    public static int daysBetween(Date startDate, Date endDate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(DATESHOWFORMAT);
        return DateUtils.daysBetween(sdf.format(startDate), sdf.format(endDate));
    }

    /**
     * 计算两个日期之间相差的天数(String类型)
     *
     * @param startDate 开始日期
     * @param endDate   结束日期
     * @return 相差天数
     */
    public static int daysBetween(String startDate, String endDate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(DATESHOWFORMAT);
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(startDate));
        long time1 = cal.getTimeInMillis();
        cal.setTime(sdf.parse(endDate));
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * 增加/减少天数
     *
     * @param date 日期
     * @param day  +/- 加/减
     * @return 日期
     */
    public static Date addDay(Date date, int day) {
        if (date == null) {
            throw new IllegalArgumentException("The date could not be null!");
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.plusDays(day).toDate();
    }
}
